<template>
    <rx-dialog @ok="onOk" @cancel="onCancel">
        <rx-layout>
            <template #middle>
                <a-tree
                    :checkedKeys="checkedKeys"
                    :treeData="treeData"
                    :default-expand-all="false"
                    :multiple="false"
                    @select="selectMenu"
                />
            </template>
        </rx-layout>
    </rx-dialog>
</template>

<script>
import { Util,RxDialog,RxLayout} from '@lowcode/jpaas-base-lib';
import InsPortalDefApi from "@/common/api/portal/insPortalDef.js";
import useMain from "../../../../../../stores/useMain";
import { mapState } from 'pinia';
export default {
    name: "MenuDialog",
    components: {
        RxDialog,
        RxLayout
    },
    props:["destroy","menus"],
    data() {
        return {
            treeData:[],
            checkedKeys:[],
            selectedRows: "",
        }
    },
    computed:{
        ...mapState(useMain,['curApp']),
    },
    created() {
        this.init();
    },
    methods: {
        init(){
            if(this.menus.length>0){
                this.treeData=this.buildTree(this.menus, '0')
            }else {
                let appId = ''
                appId=this.curApp && this.curApp.appId?this.curApp.appId : "";
                InsPortalDefApi.getMenusByAppId(appId).then(res => {
                    this.treeData=this.buildTree(res,  '0')
                })
            }
        },
        buildTree(list, parentId = "0") {
            let treeList = list.filter(item => item.parentId === parentId)
                .map(item => ({
                    key: item.id,
                    value: item.id + '',
                    title: this.$i18(item.name, '父菜单名称'),
                    selectable:true,
                    icon:item.iconPc,
                    children: this.buildTree(list, item.id)
                }));
            return treeList.sort((app1,app2)=>{
                return app1.sn - app2.sn;
            });
        },
        selectMenu(checkedKeys,e){
            this.checkedKeys=checkedKeys;
            if(this.checkedKeys.length>0){
                this.selectedRow=e.selectedNodes[0];
            }else {
                this.selectedRow="";
            }
        },
        onOk(e){
            if (!this.selectedRow) {
                this.$message.warning(this.$ti18('rxMsa.masWarn.selectOneRecord', '请选择一行记录!'));
                e.loading = false;
                return;
            }
            Util.closeWindow(this,'ok',this.selectedRow);
        },
        onCancel(){
            Util.closeWindow(this,'cancel');
        }
    },
    watch: {},
}
</script>

<style scoped>

</style>
